package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 线索跟进记录Service业务层处理
 *
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {
    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;
    @Autowired
    private TbClueTrackRecordMapper clueTrackRecordMapper;
    @Autowired
    private TbClueMapper tbClueMapper;
    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 查询商机跟进记录
     *
     * @param clueId
     * @return
     */
    @Override
    public TableDataInfo getList(Long clueId) {
        List<TbClueTrackRecord> list = tbClueTrackRecordMapper.getList(clueId);
        TableDataInfo tableDataInfo = new TableDataInfo(list, list.size());
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }

    /**
     * 添加商机跟进记录
     *
     * @param tbClueTrackRecord
     */
    @Override
    public void add(ClueTrackRecordVo tbClueTrackRecord) {
        TbClueTrackRecord record = new TbClueTrackRecord();
        BeanUtils.copyProperties(tbClueTrackRecord, record);
        record.setCreateTime(new Date());
        String username = SecurityUtils.getUsername();
        record.setCreateBy(username);
        TbClue tbClue = new TbClue();
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        BeanUtils.copyProperties(tbClueTrackRecord, tbClue);
        tbClue.setId(tbClueTrackRecord.getClueId());
        tbClueMapper.updateTbClue(tbClue);
        clueTrackRecordMapper.save(record);
    }

    /**
     * 伪线索添加三次以上删除线索
     *
     * @param id
     */
    @Override
    public void falseClue(Long id, Map map) {
        String reason = (String) map.get("reason");
        String remark = (String) map.get("remark");
        String dictValue = dictDataMapper.selectDictValue("reasons_for_reporting", reason);
        String username = SecurityUtils.getUsername();
        TbClueTrackRecord record = new TbClueTrackRecord();
        record.setCreateBy(username);
        record.setCreateTime(new Date());
        record.setClueId(id);
        record.setNextTime(null);
        record.setRecord(remark);
        record.setFalseReason(dictValue);
        record.setType("1");
        tbClueTrackRecordMapper.save(record);
        TbClue tbClue = tbClueMapper.selectTbClueById(id);
        tbClue.setStatus(TbClue.StatusType.FALSE.getValue());
        tbClueMapper.updateTbClue(tbClue);
        List<TbClueTrackRecord> list = tbClueTrackRecordMapper.getList(id);
        int sum = 0;
        for (TbClueTrackRecord tbClueTrackRecord : list) {
            if (tbClueTrackRecord.getType().equals("1"))sum++;
        }
        if (sum == 3)tbClueMapper.deleteTbClueById(id);

    }
}
